//const KEYWORD = "¥";
//const KEYWORD = "去完成";
//const KEYWORD = "控制中心";
//const KEYWORD = "金币";
//const KEYWORD_MATCH = /^(浏览).*(\d+\))$/;
//const KEYWORD_MATCH = /^(浏览)\d+s.*\d+(金币)$/;
//onst KEYWORD_MATCH = /^(.*浏览).*(\d+爆竹)$/;
//const KEYWORD_MATCH = /^(.*浏览).*(\d+金币)$/;
//const KEYWORD_MATCH = /.*(\d+肥料)$/;
const KEYWORD_MATCH = /.*(15[s|秒]).*/;

const Indent = [
  "",
  "├─",
  "│ ├─",
  "│ │ ├─",
  "│ │ │ ├─",
  "│ │ │ │ ├─",
  "│ │ │ │ │ ├─"
];

let recursionTimes = 0; // 递归次数

main();

function main() {
  auto.waitFor();

  //queryWidget(2);
  matchesWidget(1);
}

function queryWidget(level) {
  if (textContains(KEYWORD).exists()) {
    sleep(500);
    let widgets = textContains(KEYWORD).find();
    for (let index = 0; index < widgets.length; index++) {
      let widget = widgets[index];
      let level = arguments[0] ? arguments[0] : 0;
      while (level > 0) {
        level--;
        widget = widget.parent();
      }
      log("┌──────────────────────────────────────────────────────────┐");
      recursionTimes = 0;
      printWidget(widget);
      printWidgetRecursion(widget);
      log("└──────────────────────────────────────────────────────────┘");
    }
  }
}

function matchesWidget(level) {
  if (textMatches(KEYWORD_MATCH).exists()) {
    sleep(500);
    let widgets = textMatches(KEYWORD_MATCH).find();
    for (let index = 0; index < widgets.length; index++) {
      let widget = widgets[index];
      let level = arguments[0] ? arguments[0] : 0;
      while (level > 0) {
        level--;
        widget = widget.parent();
      }
      log("┌──────────────────────────────────────────────────────────┐");
      recursionTimes = 0;
      printWidget(widget);
      printWidgetRecursion(widget);
      log("└──────────────────────────────────────────────────────────┘");
    }
  }
}

function printWidgetRecursion(widget) {
  recursionTimes++;
  let childrens = widget.children();
  //log("level:" + recursionTimes + " childCount:" + childrens.length);
  for (let i = 0; i < childrens.length; i++) {
    let child = childrens[i];
    printWidget(child, recursionTimes);
    if (child.childCount() > 0) {
      printWidgetRecursion(child);
    }
  }
  recursionTimes--;
}

function printWidget(widget, level) {
  let level = arguments[1] ? arguments[1] : 0; // 设置level默认值为0

  let output = Indent[level] +
    "className: " + widget.className() + // 类名
    //"packageName: " + widget.packageName() + // 包名
    ", text: " + widget.text() + // text
    //", drawingOrder: " + widget.drawingOrder() + // 控件在父控件的绘制顺序
    ", indexInParent: " + widget.indexInParent() + // 控件在父控件的位置
    //", clickable: " + widget.clickable() + // 控件是否可点击
    //", longClickable: " + widget.longClickable() + // 控件是否可长按
    //", checkable: " + widget.checkable() + // 控件是否可勾选
    //", checked: " + widget.checked() + // 控件是否可已勾选
    //", scrollable: " + widget.scrollable() + // 控件是否可滑动
    //", selected: " + widget.selected() + // 控件是否已选择
    //", editable: " + widget.editable() + // 控件是否可编辑
    //", visibleToUser: " + widget.visibleToUser() + // 控件是否可见
    //", enabled: " + widget.enabled() + // 控件是否已启用
    //", depth: " + widget.depth() + // 控件的布局深度
    ", boundsInScreen: " + widget.bounds(); // 控件在屏幕上的范围

  log(output);
}
